<!--
数据源-接口
-->
<template>
  <div>
    <el-form v-model="form" label-width="100px">
      <el-form-item label="接口来源">
        <el-radio-group v-model="form.serviceType" @change="onServiceTypeChange">
          <el-radio label="defined">预定义</el-radio>
          <el-radio label="custom">自定义</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="isCustomService" label="请求方式">
        <el-select v-model="form.requestMethod" placeholder="请选择" style="width: 100px">
          <el-option label="get" value="get"></el-option>
          <el-option label="post" value="post"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="isCustomService" label="请求地址">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item v-else label="选择接口">
        <el-select clearable
                   v-model="form.code"
                   filterable
                   reserve-keyword
                   placeholder="请选择"
                   @change="serviceSelectChange">
          <el-option
            v-for="item in serviceList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请求参数">
        <el-table size="mini" :data="form.paramList" border>
          <el-table-column label="键" width="150">
            <template slot-scope="scope">
              <el-input v-if="isCustomService" v-model="scope.row.code"></el-input>
              <span v-else>{{scope.row.code}}</span>

            </template>
          </el-table-column>
          <el-table-column label="值" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.value">
                <el-select v-model="scope.row.type" slot="prepend" placeholder="请选择" style="width: 100px">
                  <el-option label="静态值" value=""></el-option>
                  <el-option label="动态参数" value="&"></el-option>
                  <el-option label="组件参数" value="#"></el-option>
                </el-select>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="" width="50">
            <template slot-scope="scope">
              <el-button type="text" @click="handleParamDel(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-link v-if="isCustomService" type="primary" @click="handleParamAdd" :underline="false">+增加参数</el-link>

      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import serviceList from "./service";
  import handler from './handler'

  export default {
    name: 'vpd-action-interface',
    title: '接口调用',
    handler: handler,
    props: {
      data: {
        type: Object
      }
    },
    data() {
      return {
        serviceList: serviceList,
        staticParams: '{\n}',
        form: {
          serviceType: 'defined',
          code: '',
          paramList: [],
          requestMethod: 'post',
          url: ''
        }
      }
    },
    computed: {
      isCustomService() {
        return this.form.serviceType === 'custom';
      }
    },
    beforeMount() {
      Object.assign(this.form, this.data);
    },
    methods: {
      onServiceTypeChange(val) {
        this.form.code = '';
        this.form.url = '';
        this.form.paramList = [];
      },
      handleParamAdd() {
        this.form.paramList.push({
          type: ''
        })
      },
      handleParamDel(index) {
        this.form.paramList.splice(index, 1);
      },
      getForm() {
        return this.form;
      },
      serviceSelectChange(val) {
        if (val) {
          const service = serviceList.find(item => item.code === val);
          this.form.paramList = service.params.map(item => {
            return {
              code: item.code,
              type: '',
              value: ''
            }
          })
        } else {

        }
      },

    },

  }
</script>
